package com.vieewer.btdemo.view.travel

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v4.content.ContextCompat
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import com.lindroid.androidutilskt.extension.getSp
import com.vieewer.btdemo.R
import com.vieewer.btdemo.base.BaseActivity
import com.vieewer.btdemo.bean.ChuanSceneBean
import com.vieewer.btdemo.common.Constants
import com.vieewer.btdemo.utils.ScreenSleepUtil
import com.vieewer.btdemo.utils.UIUtils
import com.vieewer.btdemo.view.compass.CompassManager
import kotlinx.android.synthetic.main.activity_travel.*
import java.lang.ref.WeakReference

class TravelActivity : BaseActivity() {

    var mChuanSceneBean : ChuanSceneBean ?= null
    var mCurCheckedPos = 0
    var mRealPos = 0

    companion object {
        const val MSG_REFRESH_TRAVEL = 0
        var mHandler : MyHandler? = null

        fun refreshView(data : ChuanSceneBean) {
            if (data != null && mHandler != null) {
                var msg = mHandler!!.obtainMessage()
                msg.what = MSG_REFRESH_TRAVEL
                var bundle = Bundle()
                bundle.putSerializable("data", data)
                msg.data = bundle
                mHandler!!.sendMessage(msg)
            }

        }

    }



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_travel)

        CompassManager.getNewInstance().setSwitchBtn(true)
        initViews()
    }


    override fun onResume() {
        super.onResume()
        var sleepTime = getSp(Constants.SP_KEY_COMMON_SLEEP_SCREEN_TIME, 15)
        ScreenSleepUtil.setScreenSleepTime(sleepTime*1000, this@TravelActivity)
    }

    private fun initViews() {
        mHandler = MyHandler(this@TravelActivity)

        var data = intent.getSerializableExtra("data")
        if (data != null) {
            mChuanSceneBean = data as ChuanSceneBean
            refreshView(mChuanSceneBean!!)
        } else {
            txt_two.text = "暂无景点信息"
        }

        txt_goto.setOnClickListener {
            gotoDetail()
        }

    }

    fun refreshView(bean: ChuanSceneBean) {
        mChuanSceneBean = bean
        if (bean.scenceBeans != null && bean.scenceBeans.size > 0) {

            var curBean = bean.scenceBeans[0]
            if (curBean != null) {

                if (curBean.distance != null) {
                    txt_one_distance.text = curBean.distance.toInt().toString() + "米"
                }

                if (curBean.orientation != null) {
                    img_one_arrow.visibility = View.VISIBLE
                    var radio : Int = UIUtils.getAngle(curBean.orientation)
                    UIUtils.startPropertyAnim(img_one_arrow, radio.toFloat())
                } else {
                    img_one_arrow.visibility = View.GONE
                }

                txt_one.text = curBean.scenceBean.title
                txt_two.text = ""
            }

            if (bean.scenceBeans.size > 1) {
                var secondBean = bean.scenceBeans[1]
                if (secondBean != null) {
                    if (secondBean.distance != null) {
                        txt_two_distance.text = secondBean.distance.toInt().toString() + "米"
                    }

                    if (secondBean.orientation != null) {
                        img_two_arrow.visibility = View.VISIBLE
                        var radio : Int = UIUtils.getAngle(secondBean.orientation)
                        UIUtils.startPropertyAnim(img_two_arrow, radio.toFloat())
                    } else {
                        img_two_arrow.visibility = View.GONE
                    }

                    txt_two.text = secondBean.scenceBean.title
                }
            }

            if (bean.scenceBeans.size > 2) {
                var thirdBean = bean.scenceBeans[2]
                if (thirdBean != null) {
                    if (thirdBean.distance != null) {
                        txt_three_distance.text = thirdBean.distance.toInt().toString() + "米"
                    }

                    if (thirdBean.orientation != null) {
                        img_three_arrow.visibility = View.VISIBLE
                        var radio : Int = UIUtils.getAngle(thirdBean.orientation)
                        UIUtils.startPropertyAnim(img_three_arrow, radio.toFloat())
                    } else {
                        img_three_arrow.visibility = View.GONE
                    }


                    txt_three.text = thirdBean.scenceBean.title
                }

            }

        } else if (bean.scenceBeans != null && bean.scenceBeans.size == 0) {
            two_left_view.visibility = View.GONE
            two_right_view.visibility = View.GONE
            img_two_arrow.visibility = View.GONE
            txt_two_distance.visibility = View.GONE
            txt_two.text = "暂无景点信息"
            one_lay.visibility = View.GONE
            three_lay.visibility = View.GONE
        }

    }


    /**
     * 声明一个静态的Handler内部类，并持有外部类的弱引用
     */
    class MyHandler constructor(activity: TravelActivity) : Handler() {

        private val mActivity: WeakReference<TravelActivity> = WeakReference<TravelActivity>(activity)

        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            when (msg!!.what) {

                MSG_REFRESH_TRAVEL -> {
                    if (mActivity != null && mActivity.get() != null && msg!!.data != null) {
                        mActivity.get()!!.refreshView(msg!!.data.getSerializable("data") as ChuanSceneBean)
                    }
                }

            }
        }

    }



    override fun onDestroy() {
        super.onDestroy()
        CompassManager.getNewInstance().setSwitchBtn(false)
        mHandler!!.removeCallbacksAndMessages(null)

    }





    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {

        Log.i("yjj", "TravelActivity onKeyDown >>>keyCode" + keyCode + ", action =" + event!!.action)
        if (keyCode == 66) {
            gotoDetail()
        }

        return super.onKeyDown(keyCode, event)
    }

    private fun gotoDetail() {
        if (mChuanSceneBean != null && mChuanSceneBean!!.scenceBeans != null) {
            if (mChuanSceneBean!!.scenceBeans.size == 1) {
                mRealPos = 0
            }
            CompassManager.getNewInstance().setSwitchBtn(false)
            var intent = Intent(this, TravelDetailActivity::class.java)
            intent.putExtra("data", mChuanSceneBean!!.scenceBeans[mRealPos])
            startActivityForResult(intent, 0)
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 0 ) {
            CompassManager.getNewInstance().setSwitchBtn(true)
            Log.i("yjj", "----------fanhuijingdian")
        }
    }


    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {

        Log.i("yjj", "TravelActivity onKeyUp >>>keyCode" + keyCode + ", action =" + event!!.action)

        when(keyCode) {
            119 -> {
                Log.i("yjj", "TravelActivity Fn按键对应键值为119")
            }
            141 -> {
                Log.i("yjj", "TravelActivity 触控长按按下对应键值为141")
            }

            142 -> {
                Log.i("yjj", "TravelActivity 触控长按松开对应键值为142")
            }


            22 -> {
                mCurCheckedPos++
                Log.i("yjj", "TravelActivity 触控长按松开对应键值为22$mCurCheckedPos")
                setCheckedView()
            }
            21 -> {
                mCurCheckedPos--
                Log.i("yjj", "TravelActivity 触控长按松开对应键值为21$mCurCheckedPos")
            }


        }

        return super.onKeyUp(keyCode, event)
    }

    private fun setCheckedView() {
        if (mChuanSceneBean != null && mChuanSceneBean!!.scenceBeans != null) {
            if (mChuanSceneBean!!.scenceBeans.size == 3) {
                mRealPos = mCurCheckedPos%3
            }

            if (mChuanSceneBean!!.scenceBeans.size == 2) {
                mRealPos = mCurCheckedPos%2
            }

        }

        Log.i("yjj", "TravelActivity" + mRealPos)

        when (mRealPos) {
            0 -> {
                txt_one.setTextColor(ContextCompat.getColor(this, R.color.main_color))
                txt_two.setTextColor(ContextCompat.getColor(this, R.color.white))
                txt_three.setTextColor(ContextCompat.getColor(this, R.color.white))
            }
            1 -> {
                txt_one.setTextColor(ContextCompat.getColor(this, R.color.white))
                txt_two.setTextColor(ContextCompat.getColor(this, R.color.main_color))
                txt_three.setTextColor(ContextCompat.getColor(this, R.color.white))
            }
            2 -> {
                txt_one.setTextColor(ContextCompat.getColor(this, R.color.white))
                txt_two.setTextColor(ContextCompat.getColor(this, R.color.white))
                txt_three.setTextColor(ContextCompat.getColor(this, R.color.main_color))
            }
        }

    }


}